//
//  SBRegisterViewController.m
//  sharedBooks
//
//  Created by 赖国锋 on 2017/6/25.
//  Copyright © 2017年 XunFengKJ. All rights reserved.
//

#import "SBRegisterViewController.h"
#import "UITextField+SMKLimit.h"
#import "YJGCDTimer.h"

@interface SBRegisterViewController ()
//姓名
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
//单位
@property (weak, nonatomic) IBOutlet UITextField *danweiTextField;
//手机
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
//获取验证码
@property (weak, nonatomic) IBOutlet UIButton *getYzmBtn;
//验证码veiw
@property (weak, nonatomic) IBOutlet UIView *yzmView;
//高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *yzmViewHeightContraint;
@property (weak, nonatomic) IBOutlet UILabel *tishiLabelone;

//邮箱注册的提示
@property (weak, nonatomic) IBOutlet UILabel *tishiLabel;
//手机
@property (weak, nonatomic) IBOutlet UILabel *shoujiLabel;
//输入验证码
@property (weak, nonatomic) IBOutlet UITextField *yzmTextField;
//密码
@property (weak, nonatomic) IBOutlet UITextField *passwdTextField;
//确认密码
@property (weak, nonatomic) IBOutlet UITextField *surePasswdTextField;
//提交
@property (weak, nonatomic) IBOutlet UIButton *tijiaoBtn;
//查看密码
@property (weak, nonatomic) IBOutlet UIButton *lookBtn;

@property (weak, nonatomic) IBOutlet UIButton *lookSurePwdBtn;


@property (weak, nonatomic) IBOutlet UIView *nameView;
@property (weak, nonatomic) IBOutlet UIView *danweiView;
@property (weak, nonatomic) IBOutlet UIView *phoneView;

@property (weak, nonatomic) IBOutlet UIView *passwdView;
@property (weak, nonatomic) IBOutlet UIView *surePasswView;
@property (strong, nonatomic)UISegmentedControl *segment;
@property(nonatomic,strong)YJGCDTimer *timer;

@property (weak, nonatomic) IBOutlet UIView *safeView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeLableViewHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passwordHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *surePasswordHeight;

@end

@implementation SBRegisterViewController
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.tabBarController.tabBar.hidden = NO;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = lColorWhite;
    _timer = [[YJGCDTimer alloc]init];
}

-(void)initViews{
    [self initSegment];
    self.tishiLabel.hidden = YES;
    self.tishiLabelone.hidden = YES;
    self.tijiaoBtn.layer.cornerRadius = 22.0f;
    self.tijiaoBtn.layer.masksToBounds = YES;
    [self setViewBoard:self.nameView];
    [self setViewBoard:self.danweiView];
    [self setViewBoard:self.phoneView];
    [self setViewBoard:self.passwdView];
    [self setViewBoard:self.surePasswView];
    [self setViewBoard:self.yzmView];
    [self.lookBtn setImage:ImageNamed(@"yanjingh") forState:UIControlStateNormal];
    [self.lookBtn setImage:ImageNamed(@"yanjing") forState:UIControlStateSelected];
    [self.lookSurePwdBtn setImage:ImageNamed(@"yanjingh") forState:UIControlStateNormal];
    [self.lookSurePwdBtn setImage:ImageNamed(@"yanjing") forState:UIControlStateSelected];
    self.phoneTextField.smk_maxLength = 11;
}

-(void)setViewBoard:(UIView*)view{
    view.borderWhich = ZJViewBorderTop;
    view.borderWhich = ZJViewBorderBottom;
}

- (void)initSegment{
    
    NSArray *array = [NSArray arrayWithObjects:@"手机注册",@"邮箱注册", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment = segment;
    segment.apportionsSegmentWidthsByContent = YES;
    segment.tintColor = LCOLOROFNAV;
    [segment setWidth:90.0 forSegmentAtIndex:0];
    [segment setWidth:90.0 forSegmentAtIndex:1];
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segment;
}

-(void)change:(UISegmentedControl *)sender{
    self.nameTextField.text = nil;
    self.danweiTextField.text = nil;
    self.phoneTextField.text = nil;
    self.yzmTextField.text = nil;
    self.passwdTextField.text = nil;
    self.surePasswdTextField.text = nil;
    
    if (sender.selectedSegmentIndex == 0) {
        self.phoneTextField.smk_maxLength = 11;
        self.yzmView.hidden = NO;
        self.yzmViewHeightContraint.constant = 45;
        self.phoneTextField.placeholder = @"请输入手机号码";
        self.getYzmBtn.hidden = NO;
        self.shoujiLabel.text = @"手机";
        self.tishiLabel.hidden = YES;
        self.tishiLabelone.hidden = YES;
        self.safeView.hidden = NO;
        self.passwdView.hidden = NO;
        self.surePasswView.hidden = NO;
        self.safeLableViewHeight.constant = 80;
        self.passwordHeight.constant = 45;
        self.surePasswordHeight.constant = 45;
    }else if (sender.selectedSegmentIndex == 1){
        self.phoneTextField.smk_maxLength = 110;
        self.yzmView.hidden = YES;
        self.getYzmBtn.hidden = YES;
        self.yzmViewHeightContraint.constant = 0;
        self.phoneTextField.placeholder = @"请输入邮箱";
        self.shoujiLabel.text = @"邮箱";
        self.tishiLabel.hidden = NO;
        self.tishiLabelone.hidden = NO;
        self.safeView.hidden = YES;
        self.passwdView.hidden = YES;
        self.surePasswView.hidden = YES;
        self.safeLableViewHeight.constant = 0;
        self.passwordHeight.constant = 0;
        self.surePasswordHeight.constant = 0;
    }
}

#pragma mark - click
//验证码
- (IBAction)verifyBtnClick:(id)sender {
    if (self.phoneTextField.text.length != 11 ) {
        [SOAutoHideMessageView showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    WEAKSELF
    [self showLoadingWithClear:YES];
//    [XFPostRequest postgetVerifyWithPhone:self.phoneTextField.text success:^(id responseObject) {
//        [weakSelf hideLoading];
//        [SOAutoHideMessageView showMessage:responseObject[@"msg"] inView:self.view];
//        if ([responseObject[@"status"] intValue] == 1003) {
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                //开启倒计时//如果会跳动，需将btn改为自定义，而不是system
//                [self startTimer];
//            });
//        }
//    } failure:^(NSError *error) {
//        [weakSelf hideLoading];
//        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:self.view];
//    }];
}

- (void)startTimer
{
    self.getYzmBtn.enabled = NO;
    WEAKSELF
    __block int count = 61;
    [_timer startWithTimeInterval:1.0 Handler:^{
        NSString *title = [NSString stringWithFormat:@"%d秒",--count];
        [weakSelf.getYzmBtn setTitle:title forState:UIControlStateDisabled];
        if (count == 0){
            [weakSelf.getYzmBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            weakSelf.getYzmBtn.enabled = YES;
            [weakSelf.timer cancel];
        }
    }];
}

-(void)dealloc{
    [_timer cancel];
}

//查看密码
- (IBAction)lookPassBtnClick:(UIButton*)sender {
    sender.selected = !sender.selected;
    self.passwdTextField.secureTextEntry = !sender.selected;
}
//查看确认密码
- (IBAction)lookSurePasswordBtnClick:(UIButton*)sender {
    sender.selected = !sender.selected;
    self.surePasswdTextField.secureTextEntry = !sender.selected;
}


@end
